package com.cn.study.service.impl;

import com.cn.study.service.GatwayRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mjy
 * @date 2021/12/29
 * @description
 */
@Service
public class GatwayRedisServiceImpl implements GatwayRedisService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void setKey(String key, String value, long expireTime, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value);
        if (expireTime > 0) {
            redisTemplate.expire(key, expireTime, timeUnit);
        }
    }

    @Override
    public void setHash(String key, Map<String, Object> map, long expireTime, TimeUnit timeUnit) {
        redisTemplate.opsForHash().putAll(key, map);
        if (expireTime > 0) {
            redisTemplate.expire(key, expireTime, timeUnit);
        }
    }

    @Override
    public Map<String, Object> getHashValue(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public String getValue(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }
}
